﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;


public partial class InventoryTracker_CustomerItem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnCustItemNew.Enabled = true;
        btnCustItemDelete.Enabled = false;
        btnCustItemEdit.Enabled = false;
        btnCustItemSubmit.Enabled = true;
        btnCustItemUpdate.Enabled = false;


    }
    protected void btnCustItemSubmit_Click(object sender, EventArgs e)
    {
        DataAccess DA = DataAccessHelper.GetDataAccess();
        CustomerItem aCustomerItem=new CustomerItem(int.Parse( ddrCustID.SelectedValue),int.Parse(txtCustItemCode.Text),int.Parse( txtCustConsumptionRate.Text));
        try
        {
            if (DA.CreateCustomerItem(aCustomerItem))
            {
                lblResult.Text = "Success";
            }
            else
            {
                lblResult.Text = "Success";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message.ToString();
        }
    }
    protected void DdlCustIDSel_SelectedIndexChanged(object sender, EventArgs e)
    {
        String SelectValue = DdlCustIDSel.SelectedValue;

        ddrCustID.Visible = false;
        txtCustIDsel.Visible = true;

        btnCustItemNew.Enabled = true;
        btnCustItemDelete.Enabled = true;
        btnCustItemEdit.Enabled = true;
        btnCustItemSubmit.Enabled = false;
        btnCustItemUpdate.Enabled = false;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM CustomerItem WHERE  [CustID]=" + SelectValue;

        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        txtCustItemCode.Text = dataTable.Rows[0]["CustItemCode"].ToString();
        txtCustConsumptionRate.Text = dataTable.Rows[0]["CustConsumptionRate"].ToString();
        
        String sqlCustIt = "SELECT * FROM CustomerMaster WHERE CustID=" + dataTable.Rows[0]["CustID"];
        SqlCommand cmdCustIt = new SqlCommand(sqlCustIt, con);
        DataTable dataTableCustIt = GetDataTable(cmdCustIt);
        txtCustIDsel.Text = dataTableCustIt.Rows[0]["CustName"].ToString();

        con.Close();
    }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }

    protected void btnCustItemNew_Click(object sender, EventArgs e)
    {
        ddrCustID.Visible= true;
        txtCustIDsel.Visible = false;

        btnCustItemNew.Enabled = false;
        btnCustItemDelete.Enabled = false;
        btnCustItemEdit.Enabled = false;
        btnCustItemSubmit.Enabled = true;
        btnCustItemUpdate.Enabled = false;

        txtCustItemCode.Text = "";
        txtCustConsumptionRate.Text = "";
        ddrCustID.SelectedIndex = 0;    
    }
    protected void btnCustItemDelete_Click(object sender, EventArgs e)
    {
        String SelectValue = DdlCustIDSel.SelectedValue;

        btnCustItemNew.Enabled = false;
        btnCustItemDelete.Enabled = true;
        btnCustItemEdit.Enabled = false;
        btnCustItemSubmit.Enabled = false;
        btnCustItemUpdate.Enabled = false;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM CustomerItem WHERE  [CustID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtCustItemCode.Text = "";
        txtCustConsumptionRate.Text = "";
        ddrCustID.SelectedIndex = 0;   

    }
    protected void btnCustItemEdit_Click(object sender, EventArgs e)
    {
        ddrCustID.Visible = true;
        txtCustIDsel.Visible = false;

        btnCustItemNew.Enabled = false;
        btnCustItemDelete.Enabled = false;
        btnCustItemEdit.Enabled = false;
        btnCustItemSubmit.Enabled = false;
        btnCustItemUpdate.Enabled = true;
               
    }

    protected void btnCustItemUpdate_Click(object sender, EventArgs e)
    {
        btnCustItemNew.Enabled = false;
        btnCustItemDelete.Enabled = false;
        btnCustItemEdit.Enabled = false;
        btnCustItemSubmit.Enabled = false;
        btnCustItemUpdate.Enabled = true;

        String SelectValue = DdlCustIDSel.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE CustomerItem SET  CustomerItemCode= '" + txtCustItemCode.Text + "' , CustConsumptionRate= '" + txtCustConsumptionRate.Text + "' WHERE CustID=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtCustItemCode.Text = "";
        txtCustConsumptionRate.Text = "";
        ddrCustID.SelectedIndex = 0;   

    }
}
